package be.tihh.mgmt.tasks;

import be.tihh.mgmt.gui.xml.app.RunCommand;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class RemoteRunner extends ComputerTask {

    RunCommand runCommand;
    private String name = "RemoteRunner";

    public RemoteRunner(RunCommand runCommand) {
        this.runCommand = runCommand;
        this.name = name + " : " + runCommand.getRunString();
    }

    public String getName() {
        return name;
    }

    public void execute() {
        super.execute();
        System.out.println("Running -->"+runCommand+"<-- on "+computer.getName());
        taskStatus.setStatus(TaskStatus.RUNNING);
        try {
            String currentDir = System.getProperty("user.dir");
            String computerName = "\\\\" + computer.getName();
            String psExecParameters = "";
            if(runCommand.getPsExecParameters()!=null) psExecParameters = " " + runCommand.getPsExecParameters();
            //todo : credentials
            String authenticationString = " -u tihh01\\testAdmin -p testPass";

            //todo : remove hardcoded test pc stuff
            //begin test pc stuff
            if("gebruiker-PC".equals(computer.getName())){
                authenticationString = " -u administrator -p testPass";
            }
            //end test pc stuff

            String runString = " "+runCommand.getRunString();
            String command = currentDir + "\\res\\util\\psexec " + computerName + psExecParameters + authenticationString + runString;
            System.out.println("command = " + command);

            Process process = Runtime.getRuntime().exec(command);
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            String standardOutput = "";
            while ((line = in.readLine()) != null) {
                standardOutput = standardOutput + line + "\n";
            }
            taskStatus.setStandardOutput(standardOutput);
            System.out.println("taskStatus.getStandardOutput() = " + taskStatus.getStandardOutput());


            in = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String errorOutput = "";
            while ((line = in.readLine()) != null) {
                errorOutput = errorOutput + line + "\n";
            }
            taskStatus.setErrorOutput(errorOutput);
            System.out.println("taskStatus.getErrorOutput() = " + taskStatus.getErrorOutput());

            process.waitFor();

            System.out.println("Exit Value = "+process.exitValue());
            if(process.exitValue()!=0){
                taskStatus.setStatus(TaskStatus.FINISHED_NOK);
            }else{
                taskStatus.setStatus(TaskStatus.FINISHED_OK);
            }
        } catch (IOException e) {
            e.printStackTrace();
            taskStatus.setStatus(TaskStatus.FINISHED_NOK);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
